﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CognisessWeb.Models;
using CognisessDA;
using CognisessService;
using CognisessCommLib;

namespace CognisessWeb.Controllers
{
    public class NavigationController : Controller
    {
        AccountBLL accountBLL = new AccountBLL();
        //
        // GET: /Navigation/

        public ActionResult Index()
        {
            List<NavigationModel> listModel = new List<NavigationModel>();
            List<Navigation> listNavigation = new List<Navigation>();
            listNavigation = accountBLL.GetNavigation(new Navigation { });
            foreach (var item in listNavigation)
            {
                NavigationModel model = new NavigationModel();
                model.Id = item.Id;
                model.Link = item.Link;
                model.ParentId = item.ParentId;
                model.DateTime = DateTime.Parse(item.DateTime.ToString());
                model.Sort = item.Sort;
                model.TitleName = item.TitleName;
                model.Type = item.Type;                
                listModel.Add(model);
            }
            return View(listModel);
        }

        //
        // GET: /Navigation/Details/5

        public ActionResult Details(int id)
        {
            List<Navigation> listNavigation = new List<Navigation>();
            listNavigation = accountBLL.GetNavigation(new Navigation { Id = id });
            NavigationModel model = new NavigationModel();
            foreach (var item in listNavigation)
            {                
                model.Id = item.Id;
                model.Link = item.Link;
                model.ParentId = item.ParentId;
                model.DateTime = DateTime.Parse(item.DateTime.ToString());
                model.Sort = item.Sort;
                model.TitleName = item.TitleName;
                model.Type = item.Type;
            }
            return View(model);
        }

        //
        // GET: /Navigation/Create

        public ActionResult Create()
        {
            List<Navigation> list = accountBLL.GetNavigation(new Navigation { ParentId = 0 }); 
            Navigation n = new Navigation();
            n.TitleName = "Select";
            n.Id = 0;
            n.Link = "";
            n.ParentId = 0;
            n.Sort = 0;
            n.Type = 0;
            n.DateTime = DateTime.Now;
            list.Insert(0, n);
            ViewData["parentId"] = new SelectList(list, "Id", "TitleName");

            ViewData["Type"] = new SelectList(XMLHelper.GetOptions("type"), "Value", "Text");

            return View();
        }

        //
        // POST: /Navigation/Create

        [HttpPost]
        public ActionResult Create(Navigation collection)
        {
            try
            {
                // TODO: Add insert logic here
                List<Navigation> list = new List<Navigation>();
                
                
                list.Add(collection);
                bool a = accountBLL.AddNavigation(list);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Navigation/Edit/5
 
        public ActionResult Edit(int id)
        {
            List<Navigation> listNavigation = new List<Navigation>();
            listNavigation = accountBLL.GetNavigation(new Navigation { Id = id });
            NavigationModel model = new NavigationModel();
            foreach (var item in listNavigation)
            {
                model.Id = item.Id;
                model.Link = item.Link;
                model.ParentId = item.ParentId;
                model.DateTime = DateTime.Parse(item.DateTime.ToString());
                model.Sort = item.Sort;
                model.TitleName = item.TitleName;
                model.Type = item.Type;
            }
            return View(model);
        }

        //
        // POST: /Navigation/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Navigation collection)
        {
            try
            {
                // TODO: Add update logic here
                List<Navigation> list = new List<Navigation>();
                list.Add(collection);
                accountBLL.UpdataNavigation(list);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Navigation/Delete/5
 
        public ActionResult Delete(int id)
        {
            accountBLL.DeleteNavigation(id);
            return View();
        }

        //
        // POST: /Navigation/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                accountBLL.DeleteNavigation(id);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
